/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/*!
 * \file test_roi_align_rotated_tiling.cpp
 * \brief
 */
#include <iostream>
#include <vector>
#include <gtest/gtest.h>
#include "../../../op_host/roi_align_rotated_tiling.h"
#include "tiling_case_executor.h"
#include "tiling_context_faker.h"

using namespace ge;
using namespace std;

class TilingForRoiAlignRotated : public testing::Test
{
protected:
  static void SetUpTestCase()
  {
    std::cout << "TilingForRoiAlignRotated SetUp" << std::endl;
  }

  static void TearDownTestCase()
  {
    std::cout << "TilingForRoiAlignRotated TearDown" << std::endl;
  }
};

TEST_F(TilingForRoiAlignRotated, roi_align_rotated_tiling_0)
{
    optiling::RoiAlignRotatedCompileInfo compileInfo = {48, 196608};
    gert::TilingContextPara tilingContextPara("RoiAlignRotated",
                                                {{{{8, 8, 8, 8}, {8, 8, 8, 8}}, ge::DT_FLOAT, ge::FORMAT_ND}, 
                                                {{{6, 8}, {6, 8}}, ge::DT_FLOAT, ge::FORMAT_ND}},
                                                {{{{8, 2, 2, 8}, {8, 2, 2, 8}}, ge::DT_FLOAT, ge::FORMAT_ND}},
                                                {gert::TilingContextPara::OpAttr("pooled_h", Ops::Cv::AnyValue::CreateFrom<int64_t>(2)),
                                                    gert::TilingContextPara::OpAttr("pooled_w", Ops::Cv::AnyValue::CreateFrom<int64_t>(2)),
                                                    gert::TilingContextPara::OpAttr("spatial_scale", Ops::Cv::AnyValue::CreateFrom<float>(0.5)),
                                                    gert::TilingContextPara::OpAttr("sampling_ratio", Ops::Cv::AnyValue::CreateFrom<int64_t>(1)),
                                                    gert::TilingContextPara::OpAttr("aligned", Ops::Cv::AnyValue::CreateFrom<bool>(false)),
                                                    gert::TilingContextPara::OpAttr("clockwise", Ops::Cv::AnyValue::CreateFrom<bool>(false)),},
                                                &compileInfo);
    uint64_t expectTilingKey = 1;
    string expectTilingData = "4294967296 8 270582939649 34359738400 34359738376 34359738376 34359738376 4539628424389459968 8589934593 2 262144 ";
    std::vector<size_t> expectWorkspaces = {0};
    ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData, expectWorkspaces);
}


// TEST_F(TilingForRoiAlignRotated, roi_align_rotated_tiling_0)
// {
//   std::string op_type("RoiAlignRotated");
//   ASSERT_NE(gert::OpImplRegistry::GetInstance().GetOpImpl(op_type.c_str()), nullptr);
//   auto tiling_func = gert::OpImplRegistry::GetInstance().GetOpImpl(op_type.c_str())->tiling;
//   auto tiling_parse_func = gert::OpImplRegistry::GetInstance().GetOpImpl(op_type.c_str())->tiling_parse;
//   string compile_info_string = R"({"hardware_info": {"BT_SIZE": 0, "load3d_constraints": "1",
//                                                        "Intrinsic_fix_pipe_l0c2out": false,
//                                                        "Intrinsic_data_move_l12ub": true,
//                                                        "Intrinsic_data_move_l0c2ub": true,
//                                                        "Intrinsic_data_move_out2l1_nd2nz": false,
//                                                        "UB_SIZE": 196608, "L2_SIZE": 33554432, "L1_SIZE": 524288,
//                                                        "L0A_SIZE": 65536, "L0B_SIZE": 65536, "L0C_SIZE": 131072,
//                                                        "CORE_NUM": 48}
//                                     })";
//   map<string, string> soc_infos;
//   map<string, string> aicore_spec;
//   map<string, string> intrinsics;
//   GetPlatFormInfos(compile_info_string.c_str(), soc_infos, aicore_spec, intrinsics);

//   // platform info
//   fe::PlatFormInfos platform_info;
//   platform_info.Init();
//   static optiling::RoiAlignRotatedCompileInfo compile_info = {40, 196352};
//   // tilingParseFunc simulate
//   auto kernel_holder =
//       gert::KernelRunContextFaker()
//           .KernelIONum(2, 1)
//           .Inputs({const_cast<char *>(compile_info_string.c_str()), reinterpret_cast<void *>(&platform_info)})
//           .Outputs({&compile_info})
//           .Build();
//   ASSERT_TRUE(kernel_holder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->Init());
//   kernel_holder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("SoCInfo", soc_infos);
//   kernel_holder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreSpec", aicore_spec);
//   kernel_holder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetCoreNumByCoreType("AICore");
//   kernel_holder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap",
//                                                                                           intrinsics);
//   ASSERT_EQ(tiling_parse_func(kernel_holder.GetContext<gert::KernelContext>()), ge::GRAPH_SUCCESS);

//   // tilingFunc simulate
//   auto param = gert::TilingData::CreateCap(4096 * 16);
//   ASSERT_NE(param, nullptr);
//   auto workspace_size_holer = gert::ContinuousVector::Create<size_t>(4096 * 16);
//   auto ws_size = reinterpret_cast<gert::ContinuousVector *>(workspace_size_holer.get());
//   gert::StorageShape input_0 = {{8, 8, 8, 8}, {8, 8, 8, 8}};
//   gert::StorageShape input_1 = {{6, 8}, {6, 8}};
//   gert::StorageShape output_shape = {{8, 2, 2, 8}, {8, 2, 2, 8}};

//   // tilingParseFunc simulate
//   auto holder =
//       gert::TilingContextFaker()
//           .NodeIoNum(2, 1)
//           .IrInstanceNum({1, 1})
//           .InputShapes({&input_0, &input_1})
//           .OutputShapes({&output_shape})
//           .CompileInfo(&compile_info)
//           .NodeAttrs({{"pooled_height", ge::AnyValue::CreateFrom<int64_t>(2)},
//                       {"pooled_width", ge::AnyValue::CreateFrom<int64_t>(2)},
//                       {"spatial_scale", ge::AnyValue::CreateFrom<float>(0.5)},
//                       {"sampling_ratio", ge::AnyValue::CreateFrom<int64_t>(1)},
//                       {"aligned", ge::AnyValue::CreateFrom<bool>(false)},
//                       {"clockwise", ge::AnyValue::CreateFrom<bool>(false)}})
//           .PlatformInfo(reinterpret_cast<char *>(&platform_info))
//           .NodeInputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND)
//           .NodeInputTd(1, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND)
//           .NodeOutputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND)
//           .TilingData(param.get())
//           .Workspace(ws_size)
//           .Build();

//   gert::TilingContext *tiling_context = holder.GetContext<gert::TilingContext>();

//   // workspaces nullptr return failed
//   EXPECT_EQ(tiling_func(tiling_context), ge::GRAPH_SUCCESS);
//   // todo check tiling result
//   auto tiling_key = tiling_context->GetTilingKey();
//   ASSERT_EQ(tiling_key, 1);
// }